#include "ThreadPool.h"

#include <iostream>
#include <functional>
#include <thread>
#include <future>
using namespace std;

int sum1(int a, int b)
{
    this_thread::sleep_for(chrono::seconds(2));
    return a + b;
}
int sum2(int a, int b, int c)
{
    this_thread::sleep_for(chrono::seconds(2));
    return a + b + c;
}

int main()
{
    ThreadPool pool;
    // pool.setMode(PoolMode::MODE_CACHED); // 设置线程池工作模式
    // pool.setThreadSizeThresHold(1024); // cached模式下可以设定线程最大数量

    pool.setTaskQueMaxThresHold(2); // 设置任务队列上限阈值

    pool.start(std::thread::hardware_concurrency()); // 获取当前CPU的核心数并为线程池设定相同数量的线程

    // C++11的future
    future<int> r1 = pool.submitTask(sum1, 1, 2);
    future<int> r2 = pool.submitTask(sum2, 1, 2, 3);
    future<double> r3 = pool.submitTask([](int b, int e)->double {
        int sum = 0;
        for (int i = b; i <= e; i++)
            sum += i;
        return sum;
        }, 1, 100);
    future<double> r4 = pool.submitTask([](int b, int e)->double {
        int sum = 0;
        for (int i = b; i <= e; i++)
            sum += i;
        return sum;
        }, 1, 100);
    future<double> r5 = pool.submitTask([](int b, int e)->double {
        int sum = 0;
        for (int i = b; i <= e; i++)
            sum += i;
        return sum;
        }, 1, 100);

    this_thread::sleep_for(chrono::seconds(5));

    cout << r1.get() << endl;
    cout << r2.get() << endl;
    cout << r3.get() << endl;
    cout << r4.get() << endl;
    cout << r5.get() << endl;

    return 0;
}
